import httpFetch from "share/httpFetch";
import config from "config";
import { formatNumber, messages } from "share/common";
import errorMessage from "share/errorMessage";
import { commonPieReportFactory, commonBarReportFactory, personnelTendencyReportFactory } from "../echarts-data-transfer";

export default {
  // 获取总金额
  getBaseAmount(params) {
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/report/api/personal/expense/analysis/report/total', params)
        .then((response) => {
          let { baseAmount } = response.data || {};
          let dataLength = 1;
          let options = [{
            title: messages('finance-7.key194')/*总报销金额*/,
            detail: baseAmount ? formatNumber(baseAmount) : 0
          }];
          resolve({
            dataLength,
            options
          });
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },

  // 个人费用总体趋势报表查询接口：
  getExpenseAnalysisReport(params) {
    // {
    //   "reportId":10010
    //   // 还有的筛选器条件
    // }
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/report/api/personal/expense/analysis/report/total/tendency', params)
        .then((response) => {
          let res = {};
          let data = response.data || [];
          res.dataLength = data.length;
          res.options = personnelTendencyReportFactory(data);
          resolve(res);
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },

  // 人员费用明细接口：
  getReportDetailUserDetailExpense(params, pagination) {
    // {
    //   "reportId":10010
    //   // 还有的筛选器条件
    // }
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/report/api/personal/expense/analysis/report/detail/list' +
        '?page=' + pagination.page + "&size=" + pagination.size, params)
        .then((response) => {
          resolve(response);
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },

  // 获取费用分析报表明细-费用类型图表
  getReportDetailExpenseType(params) {
    // {
    //   "reportId":10010
    //   // 还有的筛选器条件
    // }
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/report/api/personal/expense/analysis/report/expense/type', params)
        .then((response) => {
          let res = {};
          let data = response.data || [];
          res.dataLength = data.length;
          res.options = commonBarReportFactory(data, undefined, undefined, true);
          res.options.series[0].name = messages('finance-7.key310')/*金额(元)*/;
          res.options.legend.data = [messages('finance-7.key310')/*金额(元)*/];
          resolve(res);
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },

  // 获取费用分析报表明细-报销状态图表
  getReportDetailReportStatus(params) {
    // {
    //   "reportId":10010
    //   // 还有的筛选器条件
    // }
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/report/api/personal/expense/analysis/report/status', params)
        .then((response) => {
          let res = {};
          let data = response.data || [];
          res.dataLength = data.length;
          res.options = commonPieReportFactory(data, true);
          resolve(res);
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },
}
